package com.ctg.payroll2.service.cusadmin.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import com.ctg.payroll2.bean.po.PbCity;
import com.ctg.payroll2.bean.po.PbHeadship;
import com.ctg.payroll2.bean.po.PbProvince;
import com.ctg.payroll2.dao.cusadmin.PbHeadshipDAO;
import com.ctg.payroll2.service.cusadmin.PbHeadshipService;
import com.ctg.platform.orm.ibatis.IbatisEntityDao;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;

public class PbHeadshipServiceImpl implements PbHeadshipService {
	private PbHeadshipDAO pbHeadshipDAO;
	private IbatisEntityDao ibatisEntityDao;

	public IbatisEntityDao getIbatisEntityDao() {
		return ibatisEntityDao;
	}

	public void setIbatisEntityDao(IbatisEntityDao ibatisEntityDao) {
		this.ibatisEntityDao = ibatisEntityDao;
	}

	public PbHeadshipDAO getPbHeadshipDAO() {
		return pbHeadshipDAO;
	}

	public void setPbHeadshipDAO(PbHeadshipDAO pbHeadshipDAO) {
		this.pbHeadshipDAO = pbHeadshipDAO;
	}

	public Page getHeadship(Map parameters, int n, int m) {
		return pbHeadshipDAO.pagedQuery(parameters, n, m);
	}

	public void insertByJson(String headship) {

	}

	public void updateByJson(String headship) {

		/*
		 * {"headship":{"headship_id":"10711","headship_name":"山西省","headship_code":"34","headship_pinyin":"shangxi","headship_abbr_pinyin":"sx"}}
		 * JSONObject["headship"] is not a JSONArray.
		 */
		try {
			JSONObject jsonObject;
			jsonObject = JSONObject.fromObject(headship);
//			System.out.println(headship);

			PbHeadship entry = null;
			JSONObject o = jsonObject.getJSONObject("headship");

			entry = new PbHeadship();
			entry.setHeadship_id(Parse.toInteger(o.getString("headship_id")));
			entry.setHeadship_name(o.getString("headship_name"));
			entry.setHeadship_code(o.getString("headship_code"));
			entry.setHeadship_name_en(o.getString("headship_name_en"));
			entry.setRemark(o.getString("remark"));
			// entry.setIs_valid(1);

			if (entry.getHeadship_id() == null
					|| entry.getHeadship_id().equals("")) {

				pbHeadshipDAO.insert(entry);
			} else
				pbHeadshipDAO.update(entry);

		} catch (Exception e) {
			e.printStackTrace();

		}

	}

	public void saveHeadship(PbHeadship o) {
		if (o.getHeadship_id() == null) {
			o.setIs_valid(1);
			pbHeadshipDAO.insert(o);
		} else {
			pbHeadshipDAO.update(o);
		}
	}

	public void deleteHeadship1(List data) throws SQLException {
		if (Integer.parseInt(ibatisEntityDao.getSqlMapClient().queryForObject(
				((PbHeadship) data.get(0)).getHeadship_id() + "")
				+ "") > 0) {

		}
		;
		pbHeadshipDAO.delete(data);

	}

	public boolean deleteHeadship(List data) throws SQLException {

		pbHeadshipDAO.delete(data);
		return true;
	}

	public List query(Integer headshipId, String name) {
		HashMap parameters = new HashMap();
		if (headshipId != null)
			parameters.put("headship_id", headshipId);

		List list = pbHeadshipDAO.queryForList(".getAll", parameters);

		return list;
	}

	public void saveHeadshipCity(PbCity o) {
		// TODO Auto-generated method stub

	}

	public List queryHeadship() {
		HashMap parameters = new HashMap();

		// return pbHeadshipDAO.pagedQuery(parameters, 0, 200);
		List list = pbHeadshipDAO.queryForList(".getHeadship", parameters);

		return list;
	}

}
